﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Ivory.Interface
{
    public class UserInterface 
    {
        //Fields
        private Dictionary<string, GUIComponent> _ComponentList;

        //Construct
        public UserInterface()
        {
            _ComponentList = new Dictionary<string, GUIComponent>();
        }

        //Methods
        public void AddComponent(GUIComponent component)
        {
            if (component.ID == null)
                throw new IvoryFrameworkError(Errors.ComponentHasNoID);

            if (_ComponentList.ContainsKey(component.ID))
                throw new IvoryFrameworkError(Errors.ComponentNameNotUnique(component.ID));

            _ComponentList.Add(component.ID, component);
        }
        
        public GUIComponent Get(string id)
        {
            try
            {
                return _ComponentList[id];
            }
            catch (KeyNotFoundException)
            {
                throw new IvoryFrameworkError(Errors.ComponentNotFound(id));
            }
        }
        public T Get<T>(string id)
        {
            object c = Get(id);
            return (T)c; // as T;
        }

        public void DrawGUI(SpriteBatch batch, GameTime time)
        {
            foreach (GUIComponent component in _ComponentList.Values)
            {
                if(component.IsVisible)
                    component.Render(batch, time);
            }
        }
        public void UpdateGUI(InputControl input, GameTime time)
        {
            foreach (GUIComponent component in _ComponentList.Values)
            {
                if (component.IsVisible)
                    component.Update(input, time);
            }
        }        
    }
}
